package com.ruoyi.service.service.impl;

import com.ruoyi.service.domain.ServicePackages;
import com.ruoyi.service.domain.ServicePackagessupplies;
import com.ruoyi.service.domain.ServiceSupplies;
import com.ruoyi.service.entity.packages.PCPackagesDetailReq;
import com.ruoyi.service.entity.packages.PCPackagesDetailVO;
import com.ruoyi.service.entity.packages.PCPackagesListVO;
import com.ruoyi.service.entity.packages.PCSuppliesVO;
import com.ruoyi.service.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

@Component
public class PcPackagesService {


    @Autowired
    private ServiceOrdersMapper ordersMapper;

    @Autowired
    private ServiceNurseMapper nurseMapper;

    @Autowired
    private ServiceMycartMapper mycartMapper;

    @Autowired
    private ServiceItemscomboMapper itemscomboMapper;

    @Autowired
    private ServiceComboMapper comboMapper;

    @Autowired
    private ServiceItemsMapper itemsMapper;

    @Autowired
    private ServiceAddressMapper addressMapper;

    @Autowired
    private ServicePayroadMapper payroadMapper;

    @Autowired
    private ServiceOrderscomboMapper orderscomboMapper;

    @Autowired
    private ServiceOrdersitemsMapper ordersitemsMapper;

    @Autowired
    private ServicePackagescomboMapper packagescomboMapper;

    @Autowired
    private ServicePackagesMapper packagesMapper;

    @Autowired
    private ServiceOrderspackagesMapper orderspackagesMapper;

    @Autowired
    private ServicePayMapper payMapper;

    @Autowired
    private ServiceAssessmentMapper assessmentMapper;

    @Autowired
    private ServicePictureMapper pictureMapper;

    @Autowired
    private ServiceCodeMapper codeMapper;

    @Autowired
    private ServiceRefuseMapper refuseMapper;

    @Autowired
    private ServiceOrdernurseMapper ordernurseMapper;

    @Autowired
    private ServiceServedMapper servedMapper;

    @Autowired
    private ServiceUsersMapper usersMapper;

    @Autowired
    private ServiceReasonrefuseMapper reasonrefuseMapper;

    @Autowired
    private ServiceOrderotherpayMapper orderotherpayMapper;

    @Autowired
    private ServiceTimeMapper timeMapper;

    @Autowired
    private ServiceReasonstopMapper reasonstopMapper;

    @Autowired
    private ServiceActionMapper actionMapper;

    @Autowired
    private ServiceLogMapper logMapper;

    @Autowired
    private ServiceReasonMapper reasonMapper;

    @Autowired
    private ServiceCategoryMapper categoryMapper;

    @Autowired
    private ServiceCategoryitemsMapper categoryitemsMapper;

    @Autowired
    private ServicePackagessuppliesMapper packagessuppliesMapper;

    @Autowired
    private ServiceSuppliesMapper suppliesMapper;


    @Transactional //需要
    public List<PCPackagesListVO> getPackagesList(ServicePackages servicePackages)
    {
        List<PCPackagesListVO> retList = new ArrayList<PCPackagesListVO>();
        try {
            List<ServicePackages> packagesList = packagesMapper.selectServicePackagesList(servicePackages);
            for (ServicePackages packages : packagesList) {
                PCPackagesListVO retNode = new PCPackagesListVO(packages);
                List<ServicePackagessupplies> pakSupplies = packagessuppliesMapper.selectServicePackagessuppliesByPackagesId(packages.getPackagesId());
                for (ServicePackagessupplies pakSupply : pakSupplies) {
                    if ("2".equals(pakSupply.getPackagessuppliesDelFlag())) continue;
                    ServiceSupplies supply = suppliesMapper.selectServiceSuppliesBySuppliesId(pakSupply.getPackagessuppliesSuppliesid());
                    retNode.setPackagesSupplies(retNode.getPackagesSupplies() + supply.getSuppliesName() + "*" + pakSupply.getPackagessuppliesCount() + ";");
                }
                retList.add(retNode);
            }

            return retList;

        }catch (Exception e){
            return null;
        }
    }

    @Transactional //需要
    public PCPackagesDetailVO getPackages(Long packageId)
    {
        ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packageId);
        if (packages == null){
            return null;
        }

        PCPackagesDetailVO retNode = new PCPackagesDetailVO(packages);
        List<ServicePackagessupplies> packagessupplies = packagessuppliesMapper.selectServicePackagessuppliesByPackagesId(packageId);
        for (ServicePackagessupplies packagessupply : packagessupplies)
        {
            if ("2".equals(packagessupply.getPackagessuppliesDelFlag())) continue;
            ServiceSupplies supply = suppliesMapper.selectServiceSuppliesBySuppliesId(packagessupply.getPackagessuppliesSuppliesid());
            retNode.setPackagesSupplies(retNode.getPackagesSupplies() + supply.getSuppliesName() + "*" + packagessupply.getPackagessuppliesCount() + ";");

            PCSuppliesVO suppliesVO = new PCSuppliesVO();
            suppliesVO.init(supply);
            suppliesVO.setNum(packagessupply.getPackagessuppliesCount());
            suppliesVO.setTotal(supply.getSuppliesPrice() * suppliesVO.getNum() / 100);
            retNode.setTotal(retNode.getTotal() + suppliesVO.getTotal());
            retNode.getPackagesSuppliesList().add(suppliesVO);

        }

        return retNode;
    }

    @Transactional //需要
    public Integer savePackages(PCPackagesDetailReq packageReq)
    {
        if (packageReq.getAddFlag().equals("new")){
            ServicePackages packagesNew = new ServicePackages();
            packagesNew.setPackagesCode(packageReq.getPackagesCode());
            packagesNew.setPackagesName(packageReq.getPackagesName());
            packagesNew.setPackagesPrice(packageReq.getPackagesPrice());
            packagesNew.setPackagesPerunit(packageReq.getPackagesPerunit());
            packagesNew.setPackagesCreateBy(getUsername());
            packagesNew.setPackagesCreateTime(new Date());
            packagesNew.setPackagesUpdateBy(getUsername());
            packagesNew.setPackagesUpdateTime(new Date());
            packagesNew.setPackagesUseFlag(packageReq.getPackagesUseFlag());

            packagesMapper.insertServicePackages(packagesNew);

            packagessuppliesMapper.deleteServicePackagessuppliesByPackageId(packagesNew.getPackagesId());

            for (PCSuppliesVO suppliesVO : packageReq.getPackagesSuppliesList())
            {
                ServicePackagessupplies packagessupplies = new ServicePackagessupplies();
                packagessupplies.setPackagessuppliesPackagesid(packagesNew.getPackagesId());
                packagessupplies.setPackagessuppliesSuppliesid(suppliesVO.getSuppliesId());
                packagessupplies.setPackagessuppliesCount(suppliesVO.getNum());
                packagessupplies.setPackagessuppliesPrice(suppliesVO.getSuppliesPrice());
                packagessupplies.setPackagessuppliesCreateBy(getUsername());
                packagessupplies.setPackagessuppliesCreateTime(new Date());
                packagessupplies.setPackagessuppliesUpdateBy(getUsername());
                packagessupplies.setPackagessuppliesUpdateTime(new Date());
                packagessuppliesMapper.insertServicePackagessupplies(packagessupplies);
            }

            return 0;


        }else {
            ServicePackages packagesOld = packagesMapper.selectServicePackagesByPackagesId(packageReq.getPackagesId());
            if (packagesOld == null){
                return 1;
            }
            packagesOld.setPackagesCode(packageReq.getPackagesCode());
            packagesOld.setPackagesName(packageReq.getPackagesName());
            packagesOld.setPackagesPrice(packageReq.getPackagesPrice());
            packagesOld.setPackagesPerunit(packageReq.getPackagesPerunit());
            packagesOld.setPackagesUpdateBy(getUsername());
            packagesOld.setPackagesUpdateTime(new Date());
            packagesOld.setPackagesUseFlag(packageReq.getPackagesUseFlag());

            packagesMapper.updateServicePackages(packagesOld);

            packagessuppliesMapper.deleteServicePackagessuppliesByPackageId(packagesOld.getPackagesId());

            for (PCSuppliesVO suppliesVO : packageReq.getPackagesSuppliesList())
            {
                ServicePackagessupplies packagessupplies = new ServicePackagessupplies();
                packagessupplies.setPackagessuppliesPackagesid(packagesOld.getPackagesId());
                packagessupplies.setPackagessuppliesSuppliesid(suppliesVO.getSuppliesId());
                packagessupplies.setPackagessuppliesCount(suppliesVO.getNum());
                packagessupplies.setPackagessuppliesPrice(suppliesVO.getSuppliesPrice());
                packagessupplies.setPackagessuppliesCreateBy(getUsername());
                packagessupplies.setPackagessuppliesCreateTime(new Date());
                packagessupplies.setPackagessuppliesUpdateBy(getUsername());
                packagessupplies.setPackagessuppliesUpdateTime(new Date());
                packagessuppliesMapper.insertServicePackagessupplies(packagessupplies);
            }

            return 0;

        }

    }

    @Transactional //需要
    public List<PCSuppliesVO> getSupplies()
    {
        List<ServiceSupplies> retSupplies = suppliesMapper.selectServiceSuppliesListNew();
        List<PCSuppliesVO> retList = new ArrayList<>();
        for (ServiceSupplies supplies : retSupplies) {
            PCSuppliesVO suppliesVO = new PCSuppliesVO();
            suppliesVO.init(supplies);
            retList.add(suppliesVO);
        }
        return retList;

    }



}
